package com.wc.算法提高课.D第四章_高级数据结构.线段树.维护序列;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/18 0:36
 * @description https://www.acwing.com/problem/content/1279/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static int[] w = new int[N];
    static long[] sum = new long[N << 2];
    static long[] add = new long[N << 2];
    static long[] mul = new long[N << 2];
    static int n, m, p;

    public static void main(String[] args) {
        n = sc.nextInt();
        p = sc.nextInt();
        for (int i = 1; i <= n; i++) w[i] = sc.nextInt();
        build(1, 1, n);
        m = sc.nextInt();
        while (m-- > 0) {
            int op = sc.nextInt(), l = sc.nextInt(), r = sc.nextInt(), d;
            if (op == 1) {
                d = sc.nextInt();
                modify(1, 1, n, l, r, 0, d);
            } else if (op == 2) {
                d = sc.nextInt();
                modify(1, 1, n, l, r, d, 1);
            } else {
                out.println(query(1, 1, n, l, r));
            }
        }
        out.flush();
    }

    static void build(int u, int l, int r) {
        if (l == r) {
            sum[u] = w[l];
            mul[u] = 1;
            return;
        }
        mul[u] = 1;
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        pushUp(u);
    }

    static long query(int u, int l, int r, int ql, int qr) {
        if (l == ql && r == qr) return sum[u];
        pushDown(u, l, r);
        int mid = l + r >> 1;
        if (qr <= mid) return query(u << 1, l, mid, ql, qr);
        if (ql > mid) return query(u << 1 | 1, mid + 1, r, ql, qr);

        return (query(u << 1, l, mid, ql, mid) + query(u << 1 | 1, mid + 1, r, mid + 1, qr)) % p;
    }

    static void pushUp(int u) {
        sum[u] = (sum[u << 1] + sum[u << 1 | 1]) % p;
    }

    static void modify(int u, int l, int r, int ql, int qr, int ADD, int MUL) {
        if (l == ql && r == qr) {
            eval(u, l, r, ADD, MUL);
            return;
        }
        pushDown(u, l, r);
        int mid = l + r >> 1;
        if (qr <= mid) modify(u << 1, l, mid, ql, qr, ADD, MUL);
        else if (ql > mid) modify(u << 1 | 1, mid + 1, r, ql, qr, ADD, MUL);
        else {
            modify(u << 1, l, mid, ql, mid, ADD, MUL);
            modify(u << 1 | 1, mid + 1, r, mid + 1, qr, ADD, MUL);
        }
        pushUp(u);
    }


    static void pushDown(int u, int l, int r) {
        int mid = l + r >> 1;
        eval(u << 1, l, mid, add[u], mul[u]);
        eval(u << 1 | 1, mid + 1, r, add[u], mul[u]);
        add[u] = 0;
        mul[u] = 1;
    }

    static void eval(int u, int l, int r, long ADD, long MUL) {
        sum[u] = (sum[u] * MUL % p + ADD * (r - l + 1) % p) % p;
        mul[u] = mul[u] * MUL % p;
        add[u] = (add[u] * MUL + ADD) % p;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
